﻿Ext.define("PSI.InPlan.MakePlanForm", {
    createUI: function (entity) {
        var me = this;

        var wnd = Ext.create("Ext.window.Window", {
            title: "下达计划",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 1000,
            height: 450,
            layout: "border",
            items: [{
                region: "north",
                layout: "fit",
                height: 350,
                xtype: "panel",
                border: 0,
                items: [me.getGrid()]
            },{
                    region: "center",
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 60,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "idList",
                            value: entity.idList
                        },
                        {
                            id: "editDispatcher",
                            name: "dispatcher",
                            fieldLabel: "调度",
                            allowBlank: false,
                            blankText: "没有输入调度",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            xtype: "psi_bc_dispatcherfield"
                        }
                    ]
                }
            ],
            buttons: [
                {
                    text: "确定",
                    formBind: true,
                    iconCls: "psi-button-ok",
                    handler: me.onOK,
                    scope: me
                },
                {
                    text: "取消", handler: function () {
                        PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                            me.wnd.close();
                        });
                    }, scope: me
                }
            ]

        });

        me.wnd = wnd;
    },

    show: function (parentForm, entity, selectedItems) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(selectedItems); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (selectedItems) {
        var me = this;

        if (selectedItems) {
            var store = me.getGrid().getStore();
            store.removeAll();
            for (var i = 0; i < selectedItems.length; i++) {
                var item = selectedItems[i].getData();
                store.add({
                    id: item.id,
                    address: item.address,
                    cntNumber: item.cntNumber,
                    cntType: item.cntType,
                    deliverDT: item.deliverDT,
                    memo: item.memo,
                    vsl: item.vsl,
                    clients: item.clients,
                    cntOwner: item.cntOwner
                });
            }
        }

        Ext.getCmp("editDispatcher").focus();
    },

    getGrid: function () {
        if (this.__grid == null) {
            this.__grid = this.createGrid();
        }

        return this.__grid;
    },

    createGrid: function () {
        var modelName = "PSIInPlanModel_MakePlan";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "vsl", "clients", "address", "cntOwner", "cntNumber",
                    "cntType", "deliverDT", "memo"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            columnLines: true,
            columns: [
                    { header: "id", dataIndex: "id", hidden: true },
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 230 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true, width: 60 },
                    { header: "送货时限", dataIndex: "deliverDT", menuDisabled: true, width: 120 },
                    { header: "备注", dataIndex: "memo", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    // private
    onOK: function () {
        var me = this;

        var f = Ext.getCmp("editForm");
        var el = f.getEl() || Ext.getBody();
        el.mask("数据保存中...");
        f.submit({
            url: "/InPlan/MakePlan",
            method: "POST",
            success: function (form, action) {
                el.unmask();
                me.wnd.close();
                me.parentForm.refreshInPlanGrid();
            },
            failure: function (form, action) {
                el.unmask();
                PSI.MsgBox.showInfo(action.result.msg, function () {
                    Ext.getCmp("editDispatcher").focus();
                });
            }
        });
    }
});